#!/bin/bash
sleep 20s
DOMAIN_URL=$1

#安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel


#下载 PCRE 安装包
cd /usr/local/src/ || exit
wget https://documentation-samples.obs.cn-north-4.myhuaweicloud.com/solution-as-code-publicbucket/solution-as-code-moudle/build-a-digital-assets-platform-based-on-MetaTown/open-source-software/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz

#编译安装
cd pcre-8.35 || exit
./configure
make && make install

#安装 Nginx
cd /usr/local/src/ || exit
wget https://documentation-samples.obs.cn-north-4.myhuaweicloud.com/solution-as-code-publicbucket/solution-as-code-moudle/build-a-digital-assets-platform-based-on-MetaTown/open-source-software/nginx-1.7.8.tar.gz
tar zxvf nginx-1.7.8.tar.gz
cd nginx-1.7.8 || exit

#编译安装
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make
make install

#修改conf配置文件
cat > /usr/local/webserver/nginx/conf/nginx.conf << EOF

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    client_max_body_size 10m;
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
    #                  '\$status $body_bytes_sent "\$http_referer" '
    #                  '"\$http_user_agent" "\$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  200;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        # ssl false;

        #ssl_certificate /data01/nginx/html/tlscert.pem;

        #ssl_certificate_key /data01/nginx/html/tlskey.pem;

        #ssl_prefer_server_ciphers on;

        #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        #ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4"; keepalive_timeout 70;

        #ssl_session_cache shared:SSL:10m;

        #ssl_session_timeout 10m;

        gzip on;  #是否开启gzip模块 on表示开启 off表示关闭
        gzip_buffers 4 16k;  #设置压缩所需要的缓冲区大小
        gzip_comp_level 6;  #压缩级别1-9，数字越大压缩的越好，也越占用CPU时间
        gzip_min_length 100k;  #设置允许压缩的最小字节
        gzip_http_version 1.1;  #设置压缩http协议的版本,默认是1.1
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;  #设置压缩的文件类型
        gzip_vary on;  #加上http头信息Vary: Accept-Encoding给后端代理服务器识别是否启用 gzip 压缩

        # keepalive_timeout  65;

        # gzip  on;
        #   指定允许跨域的方法，*代表所有
        add_header Access-Control-Allow-Methods *;

        #   预检命令的缓存，如果不缓存每次会发送两次请求
        add_header Access-Control-Max-Age 3600;
        #   带cookie请求需要加上这个字段，并设置为true
        add_header Access-Control-Allow-Credentials true;

        #   表示允许这个域跨域调用（客户端发送请求的域名和端口）
        #   \$http_origin动态获取请求客户端请求的域   不用*的原因是带cookie的请求不支持*号
        add_header Access-Control-Allow-Origin \$http_origin;

        #   表示请求头的字段 动态获取
        add_header Access-Control-Allow-Headers
        \$http_access_control_request_headers;


        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header x-requested-with XMLHttpRequest;

        if (\$request_method = OPTIONS){
            return 200;
        }


        location ^~ / {

            root  html;
            index  index.html index.htm;
            try_files \$uri \$uri/  /index.html;
            #/data01/nginx/html/dist/index.html;

            if (\$request_filename ~* .*/.(?:htm|html|md)$) {
                add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
            }

            if (\$request_filename ~* .*/.(?:js|css)$) {
                expires      7d;
            }

            if (\$request_filename ~* .*/.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|ico)$) {
                expires      7d;
            }

        }

        location /v1/ {
            proxy_pass   http://${DOMAIN_URL}:81;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts\$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

EOF

#安装前端代码

#下载git
yum -y install git
#下载node.js 和 angular
curl -sL https://rpm.nodesource.com/setup_14.x | bash -
#循环检测nodejs
while ! type npm >/dev/null 2>&1
do
    yum -y install nodejs
done

#循环检测angular
while [ ! -f "/usr/bin/ng" ]
do
    npm install -g @angular/cli
done

cd /usr/local || exit
git clone https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-nft-platform-frontend.git
cd huaweicloud-solution-nft-platform-frontend || exit
# 判断文件名是否需要更改
if [ -a "src/app/@core/interface/manage.Ts" ]; then
    mv src/app/@core/interface/manage.Ts src/app/@core/interface/manage.d.ts
fi

npm install 
ng build 
#把文件移到ngin下
rm -rf /usr/local/webserver/nginx/html
cp -r /usr/local/huaweicloud-solution-nft-platform-frontend/dist/digital-collection-avatar /usr/local/webserver/nginx/html


#启动 Nginx
/usr/local/webserver/nginx/sbin/nginx
